﻿using Bangle.BLL;
using Bangle.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Phone_ClassificationEdit : BasePage
{
    #region 属性
    /// <summary>
    /// Guid
    /// </summary>
    private int id
    {
        get
        {
            return base.ParseQueryInt("id", 0);
        }
    }

    /// <summary>
    /// Action
    /// </summary>
    private string Action
    {
        get
        {
            return Request.QueryString["Action"] == null ? "Add" : Request.QueryString["Action"].ToString();
        }
    }

    /// <summary>
    /// ParentId
    /// </summary>
    private int ParentId
    {
        get
        {
            return Convert.ToInt32(ViewState["ParentId"]);
        }
    }

    public string Code
    {
        get
        {
            return base.ParseQueryString("Code", "");
        }
    }

    private string pageCode
    {
        get
        {
            string str = "";
            switch (Code)
            {
                case "Screen": str = "002004"; break;
                case "Chip": str = "002005"; break;
                case "Series": str = "002006"; break;
                case "RecordType": str = "003002"; break;
                case "ProgressType": str = "003003"; break;
                case "SupplierType": str = "004001"; break;
                case "CustomerType": str = "003004"; break;
                case "SaleState": str = "005101"; break;
            }
            return str;
        }
    }
    #endregion

    T_Classification model = new T_Classification();
    T_ClassificationBLL bll = new T_ClassificationBLL();

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        setUserControlStatus();

        if (!IsPostBack)
        {
            T_Classification model = bll.GetModelList(string.Format("ClassificationCode='{0}'", Code)).FirstOrDefault();
            ViewState["ParentId"] = model.ClassificationId;
            this.lbl_Title.Text = model.ClassificationName;
            this.btn_Back.PostBackUrl = "Classification.aspx?Code=" + Code;

            if (Action == "Modify" || Action == "Detail")
            {
                getDate(id);
            }
            if (Action == "Detail")
            {
                this.txt_TypeName.Enabled = false;
                this.txt_Description.Enabled = false;

                this.btn_Modify.Visible = true;
                this.btn_Save.Visible = false;
            }
            else if (Action == "Add" || Action == "Modify")
            {
                this.txt_TypeName.Enabled = true;
                this.txt_Description.Enabled = true;

                this.btn_Modify.Visible = false;
                this.btn_Save.Visible = true;
            }

        }
    }
    #endregion

    #region 检验用户权限
    private void setUserControlStatus()
    {
        //检验用户权限
        base.CheckUserPermission(pageCode);
        this.btn_Modify.Enabled = base.CheckUserPermissionOperation(pageCode, "Add");
        this.btn_Save.Enabled = base.CheckUserPermissionOperation(pageCode, "Add") || base.CheckUserPermissionOperation(pageCode, "Edit"); ;
    }
    #endregion

    #region 绑定数据
    /// <summary>
    /// 加载数据
    /// </summary>
    /// <param name="id"></param>
    private void getDate(int id)
    {
        model = bll.GetModel(id);
        if (model == null)
        {
            return;
        }
        this.txt_TypeName.Text = model.ClassificationName;
        this.txt_Description.Text = model.Description;
    }
    #endregion

    #region 提交
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        //检验数据是否重复
        string strCheck = " ClassificationName='" + TextUtility.ReplaceText(this.txt_TypeName.Text.Trim()) + "'";
        strCheck += string.Format(" and ClassificationCode='{0}'", Code);
        if (id > 0)
        {
            strCheck += " and ClassificationId<>" + id + "";
        }

        if (bll.GetRecordCount(strCheck) > 0)
        {
            base.Alert("分类名称已存在！");
            return;
        }

        if (Action == "Modify")
        {
            model = bll.GetModel(id);
        }
        else if (Action == "Add")
        {
            model = new T_Classification();
        }
        else
        {
            base.Alert("异常操作！");
            return;
        }

        model.ClassificationName = this.txt_TypeName.Text.Trim();
        model.Description = this.txt_Description.Text.Trim();

        try
        {
            if (Action == "Modify")
            {
                //修改
                bll.Update(model);
            }
            else if (Action == "Add")
            {
                model.ClassificationCode = "";
                model.ParentId = ParentId;
                //保存
                bll.Add(model);
            }

            base.Redirect("操作成功！", "Classification.aspx?Code=" + Code);
        }
        catch (Exception ex)
        {
            base.Alert("操作失败！");
        }
    }
    #endregion

    #region 编辑按钮
    protected void btn_Modify_Click(object sender, EventArgs e)
    {
        Response.Redirect("ClassificationEdit.aspx?Action=Modify&Id=" + id + "&Code=" + Code);
    }
    #endregion


}